const app = getApp()
const CONFIG = require('../../config.js')
const WXAPI = require('apifm-wxapi')
const AUTH = require('../../utils/auth')

Page({
	data: {
    balance:0.00,
    freeze:0,
    score:0,
    ifAdmin: false,
    score_sign_continuous:0,
    rechargeOpen: false, // 是否开启充值[预存]功能
    showShopInfoCover: false,
    shopInfo:'',
    urlPage:'',
  },
  toMyTuan() {
    wx.getStorage({
      key: 'token',
      success: function(res) {
        console.log(res);
        if (res.data && res.data != '' ){
          wx.redirectTo({
            url: '/pages/mytuan/index',
          })
        }else{
          app.showLoadText('请先登录');
        }
      },
      fail(){
        app.showLoadText('请先登录');
      }
    })
  },
  toMyBill() {
    wx.redirectTo({
      url: '/pages/bill/index',
    })
  },
  closeCover() {
    this.setData({
      showShopInfoCover: false,
    })
    wx.showTabBar();
  },
  showShopInfoFun(e) {
    let that = this;
    if (app.globalData.shopNo && app.globalData.shopNo != '') {
      WXAPI.getShopAddress().then(function (res) {
        if (res.retCode === '200') {
          wx.hideTabBar();
          console.log(res.data)
          that.setData({
            shopInfo: res.data,
            showShopInfoCover: true,
          })
        } else {
          wx.showModal({
            title: '错误',
            content: res.retMsg,
            showCancel: false
          })

        }
        // that.processYunfei();
      })
    } else {
      setTimeout(function () {
        that.showShopInfoFun();
      }, 300)
    }
  },
  copyCertificateNo(e) {
    let text = e.currentTarget.dataset.text;
    app.copyText(text);
    this.closeCover();
  },
  toSelectAddress(){
    wx.getStorage({
      key: 'token',
      success: function(res) {
        console.log(res);
        if (res.data && res.data != '' ){
          wx.navigateTo({
            url: '/pages/select-address/index',
          })
        }else{
          app.showLoadText('请先登录');
        }
      },
      fail(){
        app.showLoadText('请先登录');
      }
    })
  },
	onLoad(option) {
    // let rechargeOpen = wx.getStorageSync('RECHARGE_OPEN')
    // if (rechargeOpen && rechargeOpen == "1") {
    //   rechargeOpen = true
    // } else {
    //   rechargeOpen = false
    // }
    // this.setData({
    //   rechargeOpen: rechargeOpen
    // })
    console.log(option)
    if (option && option.page != ''){
      this.setData({
        urlPage: option.page
      })
    }
    // 屏蔽分享
    wx.hideShareMenu();
	},	
  onShow() {
    const _this = this
    // const curUserInfo = wx.getStorageSync('curUserInfo')
    // if(curUserInfo){
    //   const ifAdmin = curUserInfo && !!curUserInfo.ifAdmin
    //   this.setData({
    //     ifAdmin
    //   })
    // }
    this.setData({
      version: CONFIG.version,
      vipLevel: app.globalData.vipLevel
    })
    
    AUTH.checkHasLogined().then(isLogined => {
      if (isLogined) {
        _this.setData({
          userInfo: wx.getStorageSync('userInfo'),
          curUserInfo: wx.getStorageSync('curUserInfo'),
        })
        // _this.getUserApiInfo();
        // _this.getUserAmount();
      }
    })
  },
  tokehu(){
    wx.navigateToMiniProgram({
      appId: 'wxc684f87d11fb52f6',
      path: 'pages/my/index',
      extraData: {
        foo: 'bar'
      },
      envVersion: 'release',
      success(res) {
      },
    })
  },
  onGotUserInfo(e){
    let that = this;
    if (!e.detail.userInfo) {
      wx.showToast({
        title: '您已取消登录',
        icon: 'none',
      })
      return;
    }
    if (app.globalData.isConnected) {
      wx.setStorageSync('userInfo', e.detail.userInfo)
      AUTH.login(this, e.detail.userInfo).then((res)=>{
        // wx.navigateBack({})
        // if (that.data.urlPage == 'selectAddress'){
        //   wx.navigateTo({
        //     url: '/pages/select-address/index',
        //   })
        // } else if (that.data.urlPage == 'toPay'){
        //   wx.navigateTo({
        //     url: '/pages/to-pay-order/index',
        //   })
        // }
      });
      this.onShow()
    } else {
      wx.showToast({
        title: '当前无网络',
        icon: 'none',
      })
    }
  },
  aboutUs : function () {
    // wx.showModal({
    //   title: '联系我们',
    //   content: '江苏省苏州市松泽家园8区 董先生 13451995977',
    //   showCancel:false
    // })
  },
  goShop: function(){
    wx.navigateTo({
      url: "/pages/shop/index"
    })
  },
  loginOut(){
    AUTH.loginOut()
    wx.reLaunch({
      url: '/pages/my/index'
    })
  },
  getPhoneNumber: function(e) {
    if (!e.detail.errMsg || e.detail.errMsg != "getPhoneNumber:ok") {
      wx.showModal({
        title: '提示',
        content: '无法获取手机号码:' + e.detail.errMsg,
        showCancel: false
      })
      return;
    }
    var that = this;
    // wx.setStorageSync('token', data)
    WXAPI.bindMobileWxa(wx.getStorageSync('token'), e.detail.encryptedData, e.detail.iv).then(function (res) {
      if (res.code === 10002) {
        app.goLoginPageTimeOut()
        return
      }
      if (res.code == 0) {
        wx.showToast({
          title: '绑定成功',
          icon: 'success',
          duration: 2000
        })
        that.getUserApiInfo();
      } else {
        wx.showModal({
          title: '提示',
          content: '绑定失败',
          showCancel: false
        })
      }
    })
  },
  getUserApiInfo: function () {
    var that = this;
    WXAPI.userDetail(wx.getStorageSync('token')).then(function (res) {
      if (res.code == 0) {
        let _data = {}
        _data.apiUserInfoMap = res.data
        if (res.data.base.mobile) {
          _data.userMobile = res.data.base.mobile
        }
        that.setData(_data);
      }
    })
  },
  getUserAmount: function () {
    var that = this;
    WXAPI.userAmount(wx.getStorageSync('token')).then(function (res) {
      if (res.code == 0) {
        that.setData({
          balance: res.data.balance.toFixed(2),
          freeze: res.data.freeze.toFixed(2),
          score: res.data.score
        });
      }
    })
  },
  goAsset: function () {
    wx.navigateTo({
      url: "/pages/asset/index"
    })
  },
  goScore: function () {
    wx.navigateTo({
      url: "/pages/score/index"
    })
  },
  goOrder: function (e) {
    wx.navigateTo({
      url: "/pages/order-list/index?type=" + e.currentTarget.dataset.type
    })
  }
})